<template>
  <!-- 主体区域 -->
  <section id="app">
    <!-- 输入框 -->
    <TodoHeader @addList="fn2"></TodoHeader>
    <!-- 列表区域 -->
    <TodoMain :list="list" @delList="fn1"></TodoMain>
    <!-- 统计和清空 -->
    <TodoFooter :list="list" @remList="fn3"></TodoFooter>
  </section>
</template>

<script>
import TodoFooter from "./components/TodoFooter.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoHeader from "./components/TodoHeader.vue";

export default {
  data() {
    return {
      list: JSON.parse(localStorage.getItem("list")) || [
        { id: 1, todo: "篮球" },
        { id: 2, todo: "羽毛球" },
      ],
    };
  },
  components: {
    TodoFooter,
    TodoMain,
    TodoHeader,
  },
  methods: {
    fn1(val) {
      this.list = this.list.filter((e) => e.id !== val);
    },
    fn2(val) {
      this.list.push({ id: +new Date(), todo: val });
    },
    fn3() {
      this.list = [];
    },
  },
  watch: {
    list: {
      deep: true,
      handler(val) {
        localStorage.setItem("list", JSON.stringify(val));
      },
    },
  },
};
</script>

<style>
</style>